<?php
class Logger{
	const ALL 		= 9;
	const ERROR 	= 1;
	const WARNING	= 2;
	const INFO		= 3;
	const DEBUG 	= 4;
	const VERBOSE	= 5;

	private $path, $tag, $logfile, $logLevel;

	function __construct($tag, $path) {
		$this->tag = $tag;
		$this->path = $path;
		$this->openLogfile();
	}

	function __destruct() {
		fclose($this->logfile);
	}

	//Open Logfile
	private function openLogfile(){
		$this->logfile = fopen($this->path, "a");
	}

	//Write Message to Logfile
	private function write($priority, $message){
		if ($priority<=$this->logLevel){
			$time = date('m-d-Y - H:i:s');
			fwrite($this->logfile, $time . "| $this->tag \t| ".$this->convertPrio($priority)." | " . $message . "\n");
		}

	}

	//Write Message to Logfile with priority ERROR
	public function error($message){
		$this->write(self::ERROR, $message);
	}

	//Write Message to Logfile with priority WARNING
	public function warning($message){
		$this->write(self::WARNING, $message);
	}

	//Write Message to Logfile with priority DEBUG
	public function debug($message){
		$this->write(self::DEBUG, $message);
	}

	//Write Message to Logfile with priority INFO
	public function info($message){
		$this->write(self::INFO, $message);
	}

	//Write Message to Logfile with priority VERBOSE
	public function verbose($message){
		$this->write(self::VERBOSE, $message);
	}

	//Clear Logfile
	public function clear(){
		ftruncate($this->logfile, 0);
	}

	public function setLogLevel($level){
		$this->logLevel= $level;
	}

	private function convertPrio($priority){
		switch ($priority) {
			case 0:
				return "#";
			case 1:
				return "E";
			case 2:
				return "W";
			case 3:
				return "I";
			case 4:
				return "D";
			case 5:
				return "V";
			default:
				return "?";
		}

	}

}